package jobs.job3.classloaderdemo.server.standard.bean;


import jobs.job3.classloaderdemo.server.util.Utils;

import java.io.*;

//网络io 过程用到的实体类
public class TaskBean implements Serializable {
    String className;//taskBytes 的类名
    byte[] taskBytes;//一个类定义，字节码文件的二进制内容，（任务代码、算法代码，业务代码）该类必须继承Task类
    byte[] input;//算法的输入



    private TaskBean(String className, byte[] taskBytes, byte[] input) {
        this.className = className;
        this.taskBytes = taskBytes;
        this.input = input;
    }


    public static TaskBean of(String className, byte[] taskBytes, Object input) throws IOException {
        if (input instanceof byte[]) return new TaskBean(className, taskBytes, (byte[]) input);
        else return new TaskBean(className, taskBytes, Utils.convertToBytes(input));
    }

    public byte[] serialize() throws IOException {
        return Utils.convertToBytes(this);
    }

    public static TaskBean deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        return Utils.bytesToObject(bytes);
    }

    public byte[] getTaskBytes() {
        return taskBytes;
    }

    public byte[] getInput() {
        return input;
    }

    public String getClassName() {
        return className;
    }

    public void setInput(Object input) throws IOException {
        this.input = Utils.convertToBytes(input);
    }
}
